// pages/luckyClassfli/luckyClassfli.js
const app = getApp()
var url = require('../../utils/url.js');
var cateId;
var page = 1;
var last_page = 0;
var type;
var priceType = 1;
var priceTap = 1;
var navBar = 0;
var cateId = 0;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    if (navBar == 0) {
      console.log(page)
      wx.request({
        url: url.URl + 'cate/treasure',
        data: { cate_id: cateId, page: page },
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading();
          var shopss = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shopss.length; i++) {
            console.log(i)
            var billF = (shopss[i].goods_salenum) / (shopss[i].goods.goods_storage) * 100;
            shopss[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: that.data.shops.concat(shopss)
          });
          page++;
        }
      });
    } else if (navBar == 1) {
      wx.request({
        url: url.URl + 'cate/treasure',
        data: { page: page, new: 1, cate_id: cateId },
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          var shopss = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shopss.length; i++) {
            console.log(i)
            var billF = (shopss[i].goods_salenum) / (shopss[i].goods.goods_storage) * 100;
            shopss[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: that.data.shops.concat(shopss)
          });
          page++;
        }
      });
    } else if (navBar == 2) {
      wx.request({
        url: url.URl + 'cate/treasure',
        data: { page: page, goods_salenum: 1, cate_id: cateId },
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          var shopss = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shopss.length; i++) {
            console.log(i)
            var billF = (shopss[i].goods_salenum) / (shopss[i].goods.goods_storage) * 100;
            shopss[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: that.data.shops.concat(shopss)
          });
          page++;
        }
      });
    } else if (navBar == 3) {
      console.log(priceType)
      wx.request({
        url: url.URl + 'cate/treasure',
        data: { page: page, price: priceType, cate_id: cateId },
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          var shopss = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shopss.length; i++) {
            console.log(i)
            var billF = (shopss[i].goods_salenum) / (shopss[i].goods.goods_storage) * 100;
            shopss[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shopList: that.data.shopList.concat(shopss)
          });
          page++;
        }
      });
    }

  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTab: ['全部', '最新', '热销', '价格'],
    classify:"0",
    shops:[],
    currentNavtab:0,
    listStyle: 1,
    priceType: 2,
    queS:true
  },
  onChange(event) {
    console.log(event.detail.index)
  },
  toLei(e){
    // console.log(e)
    var that=this;
    var idx = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var classify = that.data.classify;
    if (idx == classify){
      return false;
    }else{
      that.setData({
        classify: idx,
        shops: []
      })
      console.log(id)
      cateId=id;
      page=1;
      if (navBar==0){
        that.getGoodsList()
      } if (navBar == 1) {
        that.getNewGoods()
      } if (navBar ==2) {
        that.getsalenumGoods()
      } if (navBar == 3) {
        that.getPriceGoods()
      }
    }
  },
  toSearch:function(){
    wx.navigateTo({
      url: '/pages/searchList/searchList?type=2',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that=this;
    that.getcate()
  },
  getcate(){
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })

    wx.request({
      url: url.URl + 'cate/treasure/cate',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          cateId=res.data.data[0].id
          console.log(cateId)
          // that.getList()
          that.getGoodsList()
          that.setData({
            classfli:res.data.data
          })
          
        }else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
        wx.hideLoading()
      }
    })
  },
  getGoodsList() {
    var that = this;
    if (page > 1) {
      page--;
    }

    that.setData({
      queS: true
    })
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate/treasure',
      data: {cate_id: cateId,page:page},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shops.length; i++) {
            console.log(i)
            var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
            shops[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: shops
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
        wx.hideLoading()
      }
    })
  },
  getsalenumGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate/treasure',
      data: { page: page, goods_salenum: 1, cate_id: cateId },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)

        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shops.length; i++) {
            console.log(i)
            var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
            shops[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: shops
          })
          that.setData({
            queS: true
          })
        }else if(res.data.code==201){
          that.setData({
            queS: false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getNewGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate/treasure',
      data: { page: page, new: 1, cate_id: cateId },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;

        wx.hideLoading()
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shops.length; i++) {
            console.log(i)
            var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
            shops[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: shops
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getPriceGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate/treasure',
      data: { page: page, price: priceType, cate_id: cateId },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        wx.hideLoading()
        console.log(res)
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shops.length; i++) {
            console.log(i)
            var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
            shops[i].bill = billF.toFixed(2) + "%" 
          }
          that.setData({
            shops: shops
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  switchTab: function (e) {
    var that = this;
    let idx = e.currentTarget.dataset.idx;
    let currentNavtab = that.data.currentNavtab;
    if (idx == currentNavtab && idx != 3) {
      return false;
    } else {
      page = 1
      that.setData({
        shops: []
      })
      if (idx == 0) {
        navBar = 0;
        that.getGoodsList();
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 1) {
        navBar = 1;
        that.getNewGoods()
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 2) {
        navBar = 2;
        that.getsalenumGoods()
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 3) {
        navBar = 3;
        console.log(priceType)
        console.log(priceTap)
        if (priceTap == 1) {
          priceType = 1;
          that.getPriceGoods()
          that.setData({
            priceType: 1
          })
          priceTap = 2;
        } else if (priceTap == 2) {
          priceType = 0;
          that.getPriceGoods()
          that.setData({
            priceType: 0
          })
          priceTap = 1;
        }
      }
      that.setData({
        currentNavtab: idx
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})